﻿using DLog.IStorageDal;
using DLog.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DLog.SqlServerDal
{
    public class AccessDal : Repository<LogModel>, IDal
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="connectionString"></param>
        public AccessDal(string connectionString)
            : base(new DLogContext(connectionString))
        {
        }
        /// <summary>
        /// 通过日志编号查询日志模型信息
        /// </summary>
        /// <param name="logId"></param>
        /// <returns></returns>
        public LogModel SelectModel(int logId)
        {
            return base.Find(p => p.LogId == logId);
        }
        /// <summary>
        /// 通过日志等级获得所以属于这个等级的日志数据集，返回个数
        /// </summary>
        /// <param name="logLevel"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<LogModel> SelectList(int logLevel)
        {
            return base.Filter(p => p.LogLevel == logLevel).ToList();
        }
        /// <summary>
        /// 通过日志等级获得所以属于这个等级的某一页的日志数据集，返回个数
        /// </summary>
        /// <param name="logLevel"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<LogModel> SelectList(int logLevel, int pageIndex, int pageSize)
        {
            return base.Filter(p => p.LogLevel == logLevel).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
        }
        /// <summary>
        /// 通过创建的时间区间获得所以属于这个等级的某一页的日志数据集，返回个数
        /// </summary>
        /// <param name="createStartDate"></param>
        /// <param name="createEndDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<LogModel> SelectList(DateTime createStartDate, DateTime createEndDate, int pageIndex, int pageSize)
        {
            return base.Filter(p => p.CreateDate > createStartDate && p.CreateDate < createEndDate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
        }
        /// <summary>
        /// 通过创建的日志等级和时间区间获得所以属于这个等级的某一页的日志数据集，返回个数
        /// </summary>
        /// <param name="logLevel"></param>
        /// <param name="createStartDate"></param>
        /// <param name="createEndDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<LogModel> SelectList(int logLevel, DateTime createStartDate, DateTime createEndDate, int pageIndex, int pageSize)
        {
            return base.Filter(p => p.LogLevel == logLevel && p.CreateDate > createStartDate && p.CreateDate < createEndDate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
        }
        /// <summary>
        /// 通过日志编号删除日志
        /// </summary>
        /// <param name="LogId"></param>
        /// <returns></returns>
        public int Delete(int logId)
        {
            return base.Delete(p => p.LogId == logId);
        }
    }
}
